package com.qyer.lxp.service.task;

import com.qyer.lxp.common.framework.contants.QyerContants;
import com.qyer.lxp.common.framework.core.BaseSpringService;
import com.qyer.lxp.common.framework.threadpool.AbstractAroundAdviceCallable;
import com.qyer.lxp.common.framework.threadpool.CommonThreadPool;
import com.qyer.lxp.common.framework.util.QyerConfig;
import com.qyer.lxp.service.price.IPriceService;
import com.qyer.lxp.service.product.IProductService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by jiangping on 16/5/20.
 */
@Component("priceAndPickTaskService")
public class PriceAndPickTaskService extends BaseSpringService{

    @Resource
    private IProductService productService;

    @Resource
    private IPriceService priceService;

    /**
     * 定时服务类
     */
    public void startPriceAndPickTask(){
        if(QyerConfig.getInstance().getBoolean(QyerContants.QYER_PRICE_IS_FULL_UPDATE,true)){
            Map<String,String> map = productService.getProductCode(2);
            final List<String> productCodeList = new ArrayList<String>();
//            Iterator key = map.keySet().iterator();
            /*while (key.hasNext()){
                productCodeList.add((String)key.next());
            }*/
            if(null != map && map.size() > 0){
                Iterator entryIterator = map.entrySet().iterator();
                while (entryIterator.hasNext())
                {
                    Map.Entry<String,String> entry = (Map.Entry<String,String>)entryIterator.next();
                    String key = entry.getKey();
                    String value = entry.getValue();
                    if(!value.equals("OFF_SHELF")){
                        productCodeList.add(key);
                    }
                }

                CommonThreadPool.execute(new AbstractAroundAdviceCallable() {
                    @Override
                    public Object call() throws Exception {
                        productService.insertPickDropBatch(productCodeList);
                        return null;
                    }
                });

                CommonThreadPool.execute(new AbstractAroundAdviceCallable() {
                    @Override
                    public Object call() throws Exception {
                        priceService.insertBatchPrice(productCodeList);
                        return null;
                    }
                });
            }
        }else {
            Map<String,String> map = productService.getProductCode(3);
            final List<String> productCodeUpdateList = new ArrayList<String>();
            if(null != map && map.size() > 0){
                Iterator entryIterator = map.entrySet().iterator();
                while (entryIterator.hasNext())
                {
                    Map.Entry<String,String> entry = (Map.Entry<String,String>)entryIterator.next();
                    String key = entry.getKey();
                    String value = entry.getValue();
                    if(!value.equals("OFF_SHELF")){
                        productCodeUpdateList.add(key);
                    }
                }

                CommonThreadPool.execute(new AbstractAroundAdviceCallable() {
                    @Override
                    public Object call() throws Exception {
                        productService.updateBatchPickDrop(productCodeUpdateList);
                        return null;
                    }
                });

                CommonThreadPool.execute(new AbstractAroundAdviceCallable() {
                    @Override
                    public Object call() throws Exception {
                        priceService.updateBatchPrice(productCodeUpdateList);
                        return null;
                    }
                });
            }
        }
    }
}
